package cn.wolfcode.ssm.util;

import cn.wolfcode.ssm.domain.Employee;
import com.sun.net.httpserver.HttpServer;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {

    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    public static final String PERMISSION_IN_SESSION = "PERMISSION_IN_SESSION";

    // 获取session
    public static HttpSession getSession(){
       ServletRequestAttributes requestAttributes =  (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
       return requestAttributes.getRequest().getSession();
    }

    /**
     * 获取当前登陆的用户
     * @return
     */
    public  static Employee getCurrentUser(){
        return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }


    /**
     * 设置用户装到session中
     * @param employee
     */
    public static void setCurrentUser(Employee employee){
        getSession().setAttribute(EMPLOYEE_IN_SESSION,employee);
    }


    /**
     * 获取session中所有的权限表达式
     * @return
     */
    public  static List<String> getPermissions(){
        return (List<String>) getSession().getAttribute(PERMISSION_IN_SESSION);
    }


    /**
     * 设置权限表达式装到session中
     * @param permissions
     */
    public static void setPermissions(List<String> permissions){
        getSession().setAttribute(PERMISSION_IN_SESSION,permissions);
    }
}
